package cn.da.shuai.cool.finance.controller;

import cn.da.shuai.cool.common.entity.R;
import cn.da.shuai.cool.finance.model.CoolGeneralLedger;
import cn.da.shuai.cool.finance.service.ICoolGeneralLedgerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/finance/general-ledger")
@Tag(name = "总账管理")
public class CoolGeneralLedgerController {

    @Autowired
    private ICoolGeneralLedgerService generalLedgerService;

    @PostMapping("/post")
    @Operation(summary ="过账操作")
    public R<Boolean> postTransaction(@RequestBody CoolGeneralLedger ledger) {
        return R.ok(generalLedgerService.postTransaction(ledger));
    }

    @PostMapping("/close-period")
    @Operation(summary ="期末结账")
    public R<Boolean> closePeriod(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate periodEndDate) {
        return R.ok(generalLedgerService.closePeriod(periodEndDate));
    }

    @GetMapping("/account-balance")
    @Operation(summary ="获取科目余额")
    public R<String> getAccountBalance(@RequestParam String accountCode,
                                       @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
        return R.ok(generalLedgerService.getAccountBalance(accountCode, date).toString());
    }

    @GetMapping("/trial-balance")
    @Operation(summary ="获取科目余额表")
    public R<List<Map<String, Object>>> getTrialBalance(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        return R.ok(generalLedgerService.getTrialBalance(startDate, endDate));
    }

    @GetMapping("/financial-report")
    @Operation(summary ="生成会计报表")
    public R<Map<String, Object>> generateFinancialReport(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate reportDate,
            @RequestParam String reportType) {
        return R.ok(generalLedgerService.generateFinancialReport(reportDate, reportType));
    }

    @PostMapping("/reverse-entry")
    @Operation(summary ="冲销会计分录")
    public R<Boolean> reverseEntry(@RequestParam String originalVoucherNo) {
        return R.ok(generalLedgerService.reverseEntry(originalVoucherNo));
    }
}